package com.qilin.hammer;
import java.io.*;
import java.util.regex.*;

public class DirectoryViewer {

	private String regex;
	private String currentDir;
//	private String fileRegex;
//	private String strRegex;
	private String outputType;
	private Pattern filePattern;
	private Pattern keyPattern;
	
//	private class listfilter implements FilenameFilter{
//		public boolean accept(File f, String fname){
//			return Pattern.compile(regex).matcher(fname).matches();
//		}
//	}
	
	public DirectoryViewer(){}
	
	private void print(int prefixnum, String output){
		for(int i=0;i<prefixnum;i++)
			output="          "+output;
		System.out.println(output);
	}
	
	private void view(int depth, File currentFile){
		String name = currentFile.getName();
		name=currentFile.isDirectory()?"+ "+name:name;
		if(regex==null)
			print(depth, name);
		else if(currentFile.isDirectory())
			print(depth, name);
		else if(Pattern.compile(regex).matcher(name).matches())
			print(depth, name);
		if(currentFile.isDirectory()){
			File[] filelist = currentFile.listFiles();
			for(File f:filelist){			
					view(depth+1,f);
			}
		}			
	}
	
	private void constructMathcher(String fileRegex, String dirRegex){
		if(null != fileRegex) this.filePattern = Pattern.compile(fileRegex);
		if(null != dirRegex) this.keyPattern = Pattern.compile(dirRegex);
	}
	
	/**
	 * Change the directory used in viewDirectory method
	 * @param dir
	 */
	private void setDirectory(String dir){
		this.currentDir=dir.replaceAll("\\\\","/" );
	}
	
	/**
	 * list all files in the directory 
	 * @param directory
	 */
	public void viewDirectory(String directory){
		this.regex = null;
		this.setDirectory(directory);
		this.view(0, new File(this.currentDir));
	}
	
	/**
	 * list all files with names which match the given regex in the directory
	 * @param directory
	 * @param regex
	 */
	public void viewDirectory(String directory, String regex){
		this.regex=regex;
		this.setDirectory(directory);
		this.view(0, new File(this.currentDir));
	}
	
	private void lookup(File file){
		try {
			BufferedReader br = new BufferedReader(new FileReader(file.getAbsoluteFile()));
			String linetext = null;
			Matcher keyMatcher = null;
			while((linetext=br.readLine())!=null){
				keyMatcher = this.keyPattern.matcher(linetext);
				String findsubstr = null;				
				if( keyMatcher.find()){
					if(this.outputType.equals("-l")){
						findsubstr = keyMatcher.group();
						int leftto = linetext.indexOf(findsubstr);
						int tolast = leftto+findsubstr.length();
						print(0, linetext.substring(0, leftto)+"["+findsubstr+"]"+linetext.substring(tolast));
					}else{
						print(0, file.getPath());
						break;
					}
				}	
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}	
	
	private void search(File currentFile, boolean isFindInFile){
		String name = currentFile.getName();
		if(currentFile.isDirectory()){
			File[] filelist = currentFile.listFiles();
			for(File f:filelist)		
				search(f, isFindInFile);
		}else if(this.filePattern.matcher(name).matches()){
			if(isFindInFile)
				this.lookup(currentFile);
			else
				print(0, currentFile.getPath());
		}
		
	}
	
	public void findInFile(String directory, String fileRegex, String strRegex, String outputType){
		this.setDirectory(directory);
		this.constructMathcher(fileRegex, strRegex);
		this.outputType = outputType;
		this.search(new File(this.currentDir), true);
	}
	
	
	public void findFile(String directory, String fileRegex){
		this.setDirectory(directory);
		this.constructMathcher(fileRegex, null);
		this.search(new File(this.currentDir), false);
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		DirectoryViewer viewer=new DirectoryViewer();
		//viewer.viewDirectory(".");
		//viewer.viewDirectory("D:/work/sdk4test/yoda/bw",".*\\.java");
		//viewer.findInFile("D:/work/sdk4.5/yoda/yoda/bw", ".*\\.xml", "server\\.xml", "-f");
		viewer.findFile("D:/work/main/yoda/export", "server.xml");
	}

}
